#include "common.h"
#include<QStringList>

void bit_to_word( vector<bit> bit_vector , word val[] , const int array_num )
{
    for ( int i = 0 ; i < array_num ; ++i )
    {
        val[i] = 0 ;
        vector<bit>::size_type begin_index = i * (bit_vector.size()/array_num) ;
        vector<bit>::size_type end_index = (i+1) * (bit_vector.size()/array_num) ;
        for ( ; begin_index < end_index ; ++begin_index )
        {
            val[i] = (bit_vector[begin_index]<<((i+1)*(bit_vector.size()/array_num) - begin_index - 1))|val[i] ;
        }
    }
}

void bit_to_nibble( vector<bit> bit_vector , nibble val[] , const int array_num )
{
    for ( int i = 0 ; i < array_num ; ++i )
    {
        val[i] = 0 ;
        vector<bit>::size_type begin_index = i * (bit_vector.size()/array_num) ;
        vector<bit>::size_type end_index = (i+1) * (bit_vector.size()/array_num) ;
        for ( ; begin_index < end_index ; ++begin_index )
        {
            val[i] = (bit_vector[begin_index]<<((i+1)*(bit_vector.size()/array_num) - begin_index - 1))|val[i] ;
        }
    }
}

void nibble_to_bit( nibble val[] , const int array_num , vector<bit>& bit_vector )
{
    for ( int i = 0 ; i < array_num ; ++i )
    {
        for ( int j = 3 ; j >= 0 ; --j )
        {
            bit temp = (val[i]>>j) & 0x01 ;
            bit_vector.push_back( temp ) ;
        }

    }
}

void qstring_to_bitvector(QString *str, vector<bit>& bit_vector)
{
    QString TransString = str->replace("\n", " ");
    QStringList StrList = TransString.split(" ");
    int strsize = StrList.size();
    for(int i = 0; i < strsize; ++i)
    {
        QString strtemp=StrList.at(i);
        bit temp = 0;
        temp=strtemp.toInt();
        bit_vector.push_back((bit)temp);
    }
}

void bitvector_to_qstring(vector<bit> bit_vector, QString &str)
{
    unsigned int vector_size = bit_vector.size();
    for(unsigned int i = 0; i < vector_size; ++i)
    {
        bit bittemp = bit_vector.at(i);
        QString strtemp = QString::number(bittemp,10);
        str.append(strtemp);
        str.append(" ");
        if((i+1)%8 == 0)
        {
            str.append("\r\n");
        }
    }
}

void size_random(int size, word &rand_value)
{
    word x = 0;
    if(size < 32)
    {
        for(int i = 0; i < size; ++i)
        {
            x += 0x01 << i;
        }
        rand_value = rand()%x;
    }
    else if(size == 32)
    {
        for(int i = 0; i < 8; ++i)
        {
            x += 0x01 << i;
        }
        word rand_value1 = rand()%x;
        word rand_value2 = rand()%x;
        word rand_value3 = rand()%x;
        word rand_value4 = rand()%x;
        rand_value = (rand_value1 << 24)^(rand_value2 << 18)^(rand_value3<<8)^rand_value4;
    }
    else
    {
        return;
    }
}
